package com.chainyoung.component_tmperson.ui.activity.account;


import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.appcommon.entity.UploadFile;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.Bank;
import com.chainyoung.component_tmperson.entity.BankItem;

import com.chainyoung.component_tmperson.ui.adapter.BankAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.BindRecieveAccountContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.BindRecieveAccountPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindBankCardActivity extends BaseActivity<BindRecieveAccountPresenter> implements BindRecieveAccountContract.View {

    @BindView(R2.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R2.id.et_owner_name)
    EditText etOwnerName;
    @BindView(R2.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R2.id.et_branch_bank)
    EditText etBranchBank;
    @BindView(R2.id.et_bank_card_no)
    EditText etBankCardNo;


    private List<BankItem> mData = new ArrayList<>();
    private BankItem bankItem;
    private Boolean isEdit;
    private RecieveAccount account;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void initUI() {
        isEdit = getIntent().getBooleanExtra("isEdit", false);
        account = (RecieveAccount) getIntent().getSerializableExtra("accountItem");

        setCurrentTitle(isEdit ? R.string.tmperson_update_bank_card : R.string.tmperson_bind_bank_card);

        if (isEdit && account != null) {
            etOwnerName.setText(account.getName());
            etOwnerName.setSelection(account.getName().length());
            etBranchBank.setText(account.getBankaddr());
            etBankCardNo.setText(account.getBankcard());
            tvBankName.setText(account.getBank());
            bankItem = new BankItem();
            bankItem.setParentid(account.getCountry_id());
            bankItem.setId(account.getBank_id());
        }


    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_bind_bank_card;
    }

    @OnClick(R2.id.rl_select_bank)
    void onSelectBank() {
        if (CommonUtil.isFastDoubleClick()) {
            return;
        }

        if (mData.isEmpty()) {
            getBanks();
        } else {
            showBankDialog();
        }
    }

    private void getBanks() {
        mPresenter.getBanks();
    }

    @OnClick(R2.id.tv_submit)
    void submit() {
        if (checkCanSubmit()) {
            TreeMap<String, Object> map = new TreeMap<>();
            map.put("token", SPUtil.getString(LibConstant.TOKEN));
            map.put("type", 1);
            map.put("name", StringUtil.viewGetText(etOwnerName));
            map.put("country_id", bankItem.getParentid());
            map.put("bank_id", bankItem.getId());
            map.put("bankaddr", StringUtil.viewGetText(etBranchBank));
            map.put("bankcard", StringUtil.viewGetText(etBankCardNo));
            mPresenter.setRecieveAccount(map);
        }
    }

    private boolean checkCanSubmit() {
        if (StringUtil.viewGetTextIsEmpty(etOwnerName)) {
            ToastUtil.showToast(etOwnerName.getHint().toString());
            return false;
        }

        if (!StringUtil.isBankUserNameValidate(StringUtil.viewGetText(etOwnerName))){
            ToastUtil.showToast("请输入2到30位中英文姓名");
            return false;
        }

        if (!StringUtil.isBankUserNameValidate(StringUtil.viewGetText(etBranchBank))){
            ToastUtil.showToast("请输入4到30位中英文支行地址");
            return false;
        }

        if (StringUtil.viewGetTextIsEmpty(tvBankName)) {
            ToastUtil.showToast(tvBankName.getHint().toString());
            return false;
        }

        if (StringUtil.viewGetTextIsEmpty(etBranchBank)) {
            ToastUtil.showToast(etBranchBank.getHint().toString());
            return false;
        }

        if (!StringUtil.isAddrValidate(etBranchBank.getText().toString().trim())) {
            ToastUtil.showToast(getString(R.string.tmperson_pls_input_correct_addr));
            return false;
        }

        if (StringUtil.viewGetTextIsEmpty(etBankCardNo)) {
            ToastUtil.showToast(etBankCardNo.getHint().toString());
            return false;
        }

        if (!StringUtil.isBankCard(etBankCardNo.getText().toString().trim())) {
            ToastUtil.showToast(getString(R.string.tmperson_pls_input_correct_bank_card));
            return false;
        }
        return true;
    }


    @Override
    public void onSetRecieveAccountSuccess(String data) {
        ToastUtil.showToast(data);
        finish();
    }

    @Override
    public void onGetBanks(List<Bank> data) {
        if (data.isEmpty()) {
            return;
        }
        mData.clear();
        for (Bank item : data) {
            mData.addAll(item.getArr());
        }
        showBankDialog();
    }

    @Override
    public void onUpLoadCodeSuccess(UploadFile uploadFile) {

    }

    AlertDialog alertDialog;
    private BankAdapter adapter;
    private int selectIndex=-1;

    private void showBankDialog() {
        //  if (alertDialog == null) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tmperson_layout_bank_list, null);
        int width = (int) (ScreenUtils.getScreenWidth(mContext) * 0.8);
        alertDialog = new AlertDialog.Builder(mContext)
                .setView(view)
                .setCancelable(true)
                .create();
        ListView listView = view.findViewById(R.id.list_view);
        TextView btnCancel = view.findViewById(R.id.tv_cancel);
        TextView btnConfirm = view.findViewById(R.id.tv_confirm);
        adapter = new BankAdapter(mContext, mData);
        adapter.setSelectIndex(selectIndex);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectIndex(position);
                adapter.notifyDataSetChanged();
                selectIndex=position;
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bankItem = mData.get(adapter.getSelectIndex());
                tvBankName.setText(bankItem.getTitle());
                alertDialog.dismiss();
            }
        });
        //   }
        alertDialog.show();
//        WindowManager m = getWindowManager();
//        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
//        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
//        p.width = (int) (d.getWidth() * 0.8);
//        p.height = (int) (d.getWidth() * 1.4);
//        alertDialog.getWindow().setAttributes(p);     //设置生效
    }


}
